package cn.com.dstz.admin.action;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.base.action.BaseAction;
import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.DateUtils;
import cn.com.dstz.utils.RequestUtils;
import cn.com.dstz.utils.ValidUtils;
import cn.com.dstz.web.constants.Constants;

/**
 *	意见反馈管理
 *	@author 黄阳晨
 *  
 *  @author 叶
 * 
 */
public class SuggesstionAct extends BaseAction{
	
	/**
	 * @return
	 */
	public String toadd( ){
		return "add";
	}
	
	/**
	 * 添加，但不保存日志信息
	 */
	public void add( ){
		
		int validSave = validSave( );
		if( validSave > 0 ){
			this.ajaxResponse( validSave );
			return;
		}
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		Object username = admin.get( "username" );
		
		int save = baseService.save( "dstz_suggesstion", DataBaseUtils.getData( new String[]{
				"title", "content", "datetime", "ip", "adminid", "adminname"
				
		}, new Object[]{
				StringUtils.isBlank( title ) ? username + "发表意见" : title, content, DateUtils.getCurrentTime( ), RequestUtils.getIpAddr( getRequest() ), admin.get( "id" ), username
		} ) );
		
		this.ajaxResponse( save > 0 ? 0 : -1 );
	}
	
	private int validSave( ){
		if( ValidUtils.validLengthCanEmpty( title, 80 )){
			return 1;
		}
		
		if( ValidUtils.validLength( content, 3, 500 )){
			return 2;
		}
		
		return 0;
	}
	
	private String title;
	private String content;

	public String getTitle( ) {
		return title;
	}

	public void setTitle( String title ) {
		if( StringUtils.isNotBlank( title )){
			title = decode( title );
		}
		this.title = title;
	}

	public String getContent( ) {
		return content;
	}

	public void setContent( String content ) {
		if( StringUtils.isNotBlank( content )){
			content = decode( content );
		}
		this.content = content;
	}
	
	private static final long serialVersionUID = 1L;
	
}
